package com.koushikdutta.ion;

import com.koushikdutta.async.AsyncServer;
import com.koushikdutta.async.ByteBufferList;
import com.koushikdutta.async.future.Future;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.async.http.AsyncHttpRequest;
import com.koushikdutta.async.parser.ByteBufferListParser;
import com.koushikdutta.async.util.FileCache;
import com.koushikdutta.ion.bitmap.BitmapInfo;
import com.koushikdutta.ion.bitmap.PostProcess;
import com.koushikdutta.ion.bitmap.Transform;
import com.koushikdutta.ion.loader.MediaFile;
import com.koushikdutta.ion.loader.MediaFile.MediaFileType;
import java.io.File;
import java.net.URI;
import java.util.ArrayList;

class BitmapFetcher implements LoadRequestCallback {
    boolean animateGif;
    String bitmapKey;
    IonRequestBuilder builder;
    boolean deepZoom;
    String downloadKey;
    boolean hasTransforms;
    BitmapInfo info;
    ArrayList<PostProcess> postProcess;
    int resizeHeight;
    int resizeWidth;
    ArrayList<Transform> transforms;

    BitmapFetcher() {
    }

    private boolean fastLoad(String uri) {
        Ion ion = this.builder.ion;
        if (this.deepZoom) {
            if (uri != null) {
                if (uri.startsWith("file:/")) {
                    File file = new File(URI.create(uri));
                    if (!file.exists()) {
                        return false;
                    }
                    MediaFileType type = MediaFile.getFileType(file.getAbsolutePath());
                    if (type == null || !MediaFile.isVideoFileType(type.fileType)) {
                        new LoadDeepZoom(ion, this.downloadKey, this.animateGif, null, null).onCompleted(null, file);
                        return true;
                    }
                }
            }
            return false;
        }
        boolean put = !this.hasTransforms;
        for (Loader loader : ion.configure().getLoaders()) {
            Future<BitmapInfo> future = loader.loadBitmap(this.builder.contextReference.getContext(), ion, this.downloadKey, uri, this.resizeWidth, this.resizeHeight, this.animateGif);
            if (future != null) {
                final BitmapCallback callback = new LoadBitmapBase(ion, this.downloadKey, put);
                future.setCallback(new FutureCallback<BitmapInfo>() {
                    public void onCompleted(Exception e, BitmapInfo result) {
                        callback.report(e, result);
                    }
                });
                return true;
            }
        }
        return false;
    }

    public static boolean shouldDeferImageView(Ion ion) {
        if (ion.bitmapsPending.keySet().size() <= 5) {
            return false;
        }
        int loadCount = 0;
        for (String key : ion.bitmapsPending.keySet()) {
            if (ion.bitmapsPending.tag(key) instanceof LoadBitmapBase) {
                loadCount++;
                if (loadCount > 5) {
                    return true;
                }
            }
        }
        return false;
    }

    public DeferredLoadBitmap defer() {
        DeferredLoadBitmap ret = new DeferredLoadBitmap(this.builder.ion, this.downloadKey, this);
        executeTransforms(this.builder.ion);
        return ret;
    }

    private void executeTransforms(Ion ion) {
        if (this.hasTransforms && ion.bitmapsPending.tag(this.bitmapKey) == null) {
            ion.bitmapsPending.add(this.downloadKey, new TransformBitmap(ion, this.bitmapKey, this.downloadKey, this.transforms, this.postProcess));
        }
    }

    public boolean loadRequest(AsyncHttpRequest request) {
        return !fastLoad(request.getUri().toString());
    }

    public void execute() {
        final Ion ion = this.builder.ion;
        FileCache fileCache = ion.responseCache.getFileCache();
        if (this.builder.noCache || !this.hasTransforms || !fileCache.exists(this.bitmapKey) || this.deepZoom) {
            if (ion.bitmapsPending.tag(this.downloadKey) == null && !fastLoad(this.builder.uri)) {
                this.builder.setHandler(null);
                this.builder.loadRequestCallback = this;
                if (this.deepZoom) {
                    EmitterTransform<File> emitterTransform = this.builder.write(fileCache.getTempFile());
                    emitterTransform.setCallback((FutureCallback) new LoadDeepZoom(ion, this.downloadKey, this.animateGif, emitterTransform, fileCache) {
                        public void onCompleted(Exception e, File file) {
                            super.onCompleted(e, file);
                        }
                    });
                } else {
                    boolean z;
                    EmitterTransform<ByteBufferList> emitterTransform2 = this.builder.execute(new ByteBufferListParser(), new Runnable() {
                        public void run() {
                            AsyncServer.post(Ion.mainHandler, new Runnable() {
                                public void run() {
                                    ion.bitmapsPending.remove(BitmapFetcher.this.downloadKey);
                                }
                            });
                        }
                    });
                    String str = this.downloadKey;
                    if (this.hasTransforms) {
                        z = false;
                    } else {
                        z = true;
                    }
                    emitterTransform2.setCallback(new LoadBitmap(ion, str, z, this.resizeWidth, this.resizeHeight, this.animateGif, emitterTransform2));
                }
            }
            executeTransforms(ion);
            return;
        }
        TransformBitmap.getBitmapSnapshot(ion, this.bitmapKey, this.postProcess);
    }
}
